package com.dlry.db;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.dlry.entity.DlLeave;
import com.dlry.entity.DlNotification;
import com.dlry.entity.DlTravelRequest;

public class UserRepositoryImpl implements UserProperitiesPaged {
	@PersistenceContext
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public List<DlNotification> getNotificationsPagedById(Long uid, Integer from, Integer max) {
		String hql = "select n from DlLoginuser u inner join u.notifications as n where u.id = ? order by n.updatetime desc";
		Query query = entityManager.createQuery(hql);
		query.setParameter(0, uid);
		query.setFirstResult(from);
		query.setMaxResults(max);
		List<DlNotification> ns = query.getResultList();
		return ns;
	}

	@SuppressWarnings("unchecked")
	public List<DlLeave> getLeavesPagedById(Long uId, Integer from, Integer max) {
		String hql = "select l from DlLoginuser u inner join u.leaves as l where u.id = ? order by l.createdDate desc";
		Query query = entityManager.createQuery(hql);
		query.setParameter(0, uId);
		query.setFirstResult(from);
		query.setMaxResults(max);
		List<DlLeave> leaves = query.getResultList();
		return leaves;
	}

	@SuppressWarnings("unchecked")
	public List<DlTravelRequest> getTravelPagedById(Long uId, Integer from, Integer max) {
		String hql = "select t from DlLoginuser u inner join u.travels as t where u.id = ? order by t.createDate desc";
		Query query = entityManager.createQuery(hql);
		query.setParameter(0, uId);
		query.setFirstResult(from);
		query.setMaxResults(max);
		List<DlTravelRequest> travels = query.getResultList();
		return travels;
	}

}
